package com.example.invoker.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author huaiyu yin
 * @Date 2021/5/7 12:25 下午
 * @Version 1.0
 */
public class DateUtils {

    public static final  String week = "week";


    /**
     * 根据传入的参数，来对日期区间进行拆分，返回拆分后的日期List
     * @param  statisticsType , startDate,  endDate
     * @return list
     *
     * 　　这里以statisticsType参数值为：week，map中的时间参数为：{startDate=2019-08-30, endDate=2019-09-13} 为例，输出结果为：
     * 　　listWeekOrMonth值：[2019-08-30, 2019-09-01, 2019-09-02, 2019-09-08, 2019-09-09, 2019-09-13]
     * 　　这里输出的值，每两个时间为一个时间段，也就是说2019-08-30~2019-09-01为第一周，2019-09-02~2019-09-08为第二周，2019-09-09~2019-09-13为第三周。
     *
     * 　　这里以statisticsType参数值为：month，map中的时间参数为：{startDate=2019-08-30, endDate=2019-09-13} 为例，输出结果为：
     * 　　listWeekOrMonth值：[2019-08-30, 2019-08-31, 2019-09-01, 2019-09-13]
     * 　　这里输出的值，每两个时间为一个时间段，也就是说2019-08-30~2019-08-31为第一月，2019-09-01~2019-09-13为第二月。
     *
     */
    public static  List<String> doDateByStatisticsType(String statisticsType,String startDate, String endDate) throws ParseException{
        List<String> listWeekOrMonth = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = dateFormat.parse(startDate);
        Calendar sCalendar = Calendar.getInstance();
        sCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        sCalendar.setTime(sDate);
        Date eDate = dateFormat.parse(endDate);
        Calendar eCalendar = Calendar.getInstance();
        eCalendar.setFirstDayOfWeek(Calendar.MONDAY);
        eCalendar.setTime(eDate);
        boolean bool =true;
        if(statisticsType.equals("week")){
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                if(bool||sCalendar.get(Calendar.DAY_OF_WEEK)==2||sCalendar.get(Calendar.DAY_OF_WEEK)==1){
                    listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }else{
            while(sCalendar.getTime().getTime()<eCalendar.getTime().getTime()){
                if(bool||sCalendar.get(Calendar.DAY_OF_MONTH)==1||sCalendar.get(Calendar.DAY_OF_MONTH)==sCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
                    listWeekOrMonth.add(dateFormat.format(sCalendar.getTime()));
                    bool = false;
                }
                sCalendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            if(listWeekOrMonth.size()%2!=0){
                listWeekOrMonth.add(dateFormat.format(eCalendar.getTime()));
            }
        }

        return listWeekOrMonth;
    }


    public static void main(String[] args) {
        String startDate="2019-08-30";
        String endDate="2019-09-13";



        System.out.println();
    }
}
